package com.jingtai.platform.service.admin.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.enums.EnableStatusEnum;
import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.core.entity.admin.Menu;
import com.jingtai.core.entity.admin.PlatformAccount;
import com.jingtai.core.entity.admin.PlatformRole;
import com.jingtai.core.mapper.admin.MenuMapper;
import com.jingtai.core.mapper.admin.PlatformRoleMapper;
import com.jingtai.platform.service.admin.MenuService;
import com.jingtai.platform.service.admin.PlatformAccountService;
import com.jingtai.platform.service.admin.PlatformRoleService;
import com.jingtai.platform.util.ResultUtil;
import com.jingtai.platform.util.UrlToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.relation.RoleStatus;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 菜单表 impl
 *
 * @author sunlaiqian
 * @date 2020-12-02 18:31:33
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private MenuService menuService;
    @Autowired
    private PlatformAccountService platformAccountService;
    @Autowired
    private PlatformRoleService platformRoleService;

    @Override
    public ResultUtil Menulist(Integer page, Integer limit, String conditions){
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        Map<String, String> map = UrlToMap.paramToMap(conditions);
        for(Map.Entry<String, String> entry : map.entrySet()){
            wrapper.like(entry.getKey(), entry.getValue());
        }
        wrapper.eq("delete_flag", DeleteFlagEnum.NOT_DELETE.getCode());
        return new ResultUtil(ResultMapInfo.GETSUCCESS,menuService.page(new Page<>(page,limit),wrapper));
    }

    @Override
    public ResultUtil addMenu(Menu menu) {
        boolean save = menuService.save(menu);
        return new ResultUtil(save ? ResultMapInfo.ADDSUCCESS:ResultMapInfo.ADDFAIL);
    }

    @Override
    public ResultUtil getMenuById(Long id) {
        Menu menu = menuService.getById(id);
        return menu == null ?  new ResultUtil(ResultMapInfo.GETFAIL) : new ResultUtil(ResultMapInfo.GETSUCCESS,menu);
    }

    @Override
    public ResultUtil delMenuById(Long id) {
        Menu menu = new Menu();
        menu.setId(id);
        menu.setDeleteFlag(DeleteFlagEnum.DELETE.getCode());
        boolean delState = menuService.updateById(menu);
        return delState ?  new ResultUtil(ResultMapInfo.DELETESUCCESS) : new ResultUtil(ResultMapInfo.DELETEFAIL);
    }

    @Override
    public ResultUtil selectMenu(Long id) {
        PlatformAccount platformAccount = platformAccountService.getOne(Wrappers.lambdaQuery(PlatformAccount.class).eq(PlatformAccount::getId,id).eq(PlatformAccount::getEnableState, EnableStatusEnum.NORMAL.getValue()).eq(PlatformAccount::getDeleteFlag,DeleteFlagEnum.NOT_DELETE.getCode()));
        if (platformAccount == null){
            return new ResultUtil(ResultMapInfo.GETFAIL);
        }
        PlatformRole platformRole = platformRoleService.getOne(Wrappers.lambdaQuery(PlatformRole.class).eq(PlatformRole::getId,platformAccount.getRoleId()).eq(PlatformRole::getDeleteFlag,DeleteFlagEnum.NOT_DELETE.getCode()));
        if (platformRole == null){
            return new ResultUtil(ResultMapInfo.GETFAIL);
        }
        String[] split = platformRole.getMenuIds().split(",");
        List<Menu> f_id = menuService.list(Wrappers.lambdaQuery(Menu.class).eq(Menu::getLevel, 1).in(Menu::getId, split).orderByDesc(Menu::getWeight));
        List<JSONObject> list = new ArrayList<>();
        for (Menu menu : f_id) {
            List<Menu> f_id2 = menuService.list(Wrappers.lambdaQuery(Menu.class).eq(Menu::getLevel, 2).eq(Menu::getLevelOfOne, menu.getId()).in(Menu::getId, split).orderByDesc(Menu::getWeight));
            JSONObject jsonObject = new JSONObject();
            jsonObject = JSONObject.parseObject(JSON.toJSONString(menu));
            jsonObject.put("list",f_id2);
            list.add(jsonObject);
        }
        String html = "";
        for (JSONObject jsonObject : list) {
            html += "<li data-name=\"home\" class=\"layui-nav-item\">\n" +
                    "                        <a href=\"javascript:;\" lay-tips=\"" + jsonObject.get("menuName") + "\" lay-direction=\"2\">\n" +
                    "                            <i class=\"layui-icon layui-icon-table\"></i>\n" +
                    "                            <cite>" + jsonObject.get("menuName") + "</cite>\n" +
                    "                        </a>\n" +
                    "                        <dl class=\"layui-nav-child\">\n";
            int i = 0;
            for (Object obj : JSONObject.parseArray(JSON.toJSONString(jsonObject.get("list")))) {
                JSONObject jsonObject1 = JSONObject.parseObject(JSON.toJSONString(obj));
                html += "                            <dd data-name=\"console\">\n" +
                        "                                <a lay-href=\"" + jsonObject1.getString("menuUrl") + "\">" + jsonObject1.get("menuName") + "</a>\n" +
                        "                            </dd>\n";
            }
            html += "                        </dl>\n" +
                    "                    </li>";
        }
        return new ResultUtil(ResultMapInfo.GETSUCCESS,html);
    }

    @Override
    public ResultUtil selectMenuOfOne() {
        List<Menu> list = menuService.list(Wrappers.lambdaQuery(Menu.class).eq(Menu::getEnableState, EnableStatusEnum.NORMAL.getValue()).eq(Menu::getLevel, 1).eq(Menu::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
        return new ResultUtil(ResultMapInfo.GETSUCCESS,list);
    }

    @Override
    public ResultUtil selectLevelIsOneAndTwo() {
        List<JSONObject> list = new ArrayList<>();
        List<Menu> menus = menuService.list(Wrappers.lambdaQuery(Menu.class).eq(Menu::getLevel,1).eq(Menu::getEnableState,EnableStatusEnum.NORMAL.getValue()).eq(Menu::getDeleteFlag,DeleteFlagEnum.NOT_DELETE.getCode()));
        //查询二级
        for (Menu menu : menus){
            List<Menu> menus1 = menuService.list(Wrappers.lambdaQuery(Menu.class).eq(Menu::getLevel,2).eq(Menu::getLevelOfOne,menu.getId()).eq(Menu::getEnableState,EnableStatusEnum.NORMAL.getValue()).eq(Menu::getDeleteFlag,DeleteFlagEnum.NOT_DELETE.getCode()));
            JSONObject jsonObject = new JSONObject();
            jsonObject = JSONObject.parseObject(JSON.toJSONString(menu));
            jsonObject.put("list",menus1);
            list.add(jsonObject);
        }
        return new ResultUtil(ResultMapInfo.GETSUCCESS,list);
    }

    @Override
    public ResultUtil editMenu(Menu menu) {
        boolean editState = menuService.updateById(menu);
        return editState ?  new ResultUtil(ResultMapInfo.EDITSUCCESS) : new ResultUtil(ResultMapInfo.EDITFAIL);
    }

}
